<?php

require_once(dirname(__FILE__) ."/DataObjectWedWithImages.php");

/**
* $id: dbtree.class.php,v 2.1 2005/09/21 19:32:45 Kuzma Exp $
*
* CLASS DESCRIPTION:
* This class can be used to manipulate nested sets of database table
* records that form an hierarchical tree.
*
* It provides means to initialize the record tree, insert record nodes
* in specific tree positions, retrieve node and parent records, change
* position of nodes and delete record nodes.
*
*/
class TreeNode extends DataObjectWedWithImages
{
    public $_dbTable = 'tree';
    public $_dbIndex = 'nodeID';
    public $_arFields = array('name',
                              'description',
                              'active');

    // I need
    public function __construct()
    {
        array_push($this->_arFields, 'parent_nodeID');
        array_push($this->_arFields, 'tleft');
        array_push($this->_arFields, 'tright');
        array_push($this->_arFields, 'level');
        parent::__construct();
    }

    public function getNumOfChilds()
    {
        return ($this->tright - $this->tleft - 1)/2;
    }

    public function get($id=null)
    {
        if (parent::get($id))
        {
            if ($this->level>0)
            {
                $sql = 'SELECT A.'. $this->_dbIndex .'
                        FROM '. $this->_dbTable .' A, '. $this->_dbTable .' B
                        WHERE B.'. $this->_dbIndex .' = '. (int)$id .' AND B.tleft BETWEEN A.tleft AND A.tright AND A.level=\''. ($this->level-1) .'\' ORDER BY A.tleft';
                $result = mysql_query($sql, $this->_dbLink);
                list($this->parent_nodeID) = mysql_fetch_row($result);
            }
            return 1;
        }
        return 0;
    }
}

?>
